/**
 * 
 */
package gopi.labs.database.postgresql;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import gopi.labs.database.CloudSpace;
import gopi.labs.exceptions.InValidIdentifier;
import gopi.labs.exceptions.InvalidDataRecord;

/**
 * @author GS4
 * 
 */
public class HsqldbCloudSpace extends CloudSpace {

	@Override
	public void readParams() throws IOException {
		this.properties = new Properties();
		InputStream is = new FileInputStream("config.properties");
		properties.load(is);

	}

	public void validate() throws InValidIdentifier, SQLException {
		super.validate();
	
	}


	public HsqldbCloudSpace(String name) {
		super(name);

	}

	@Override
	public void update() throws SQLException, InvalidDataRecord {

		Statement stmt = this.connection.createStatement();

		if (!checkCloudExists(this.getName()))
			throw new InvalidDataRecord("Cloud name " + this.getName()
					+ " does not exist");

		 
		insertUpdateAttributes(this.getAttributes());

		stmt.close();
		this.connection.commit();
	}

	@Override
	public void create() throws SQLException, InvalidDataRecord {
		Statement stmt = this.connection.createStatement();
		ResultSet rs = stmt
				.executeQuery("select count(*) from metadata.cloud where name = '"
						+ this.getName() + "'");

		if (rs.next() && rs.getInt(1) != 0) {
			throw new InvalidDataRecord(this.getName() + " already exists");
		} else {
			rs.close();
			stmt.close();
			stmt = this.connection.createStatement();
			stmt.execute("insert into metadata.cloud (name, password) values('"
					+ this.getName() + "','" + this.getPassword() + "')");
			stmt.close();
			
			insertUpdateAttributes(this.getAttributes());
			
		}

		System.out.println("create schema " + this.getName());
		stmt = this.connection.createStatement();
		stmt.execute("create schema " + this.getName());
		stmt.close();
		this.connection.commit();
	}
	
	@Override
	public void drop() throws SQLException {
		Statement stmt = this.connection.createStatement();

		stmt.execute("delete from metadata.cloud_attributes where name = '"
				+ this.getName() + "'");
		stmt.execute("delete from metadata.cloud where name = '"
				+ this.getName() + "'");
		stmt.execute("delete from metadata.data_model_attributes where cloud_name = '"
				+ this.getName() + "'");
		stmt.execute("delete from metadata.data_model where cloud_name = '"
				+ this.getName() + "'");
		stmt.execute("delete from metadata.data_stream_attributes where cloud_name = '"
				+ this.getName() + "'");
		stmt.execute("delete from metadata.data_stream where cloud_name = '"
				+ this.getName() + "'");
		stmt.execute("delete from metadata.data_stream_mappping where cloud_name = '"
				+ this.getName() + "'");
		stmt.execute("drop schema " + this.getName() + " cascade");
		stmt.close();
		this.connection.commit();
	}
}
